package my.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
 * 获取数组的排列组合
 * @author xsw
 * @date 2017-8-11
 */
public class ArrangeUtil {
	public static List<int[]> getArrangeCompose(int length,int remove){
		int[] arr=new int[length];
		for (int i = 0; i < length; i++) {
			arr[i]=i;
		}
		List<int[]> list = new ArrayList<int[]>();
		if(remove==0){
			list.add(arr);
			return list;
		}
		arrangeCompose(arr,list, 1,remove);
		return list;
	}
	private static void arrangeCompose(int[] arr,List<int[]> list,int i,int remove){
		if(i>remove){
			list.add(getList(arr, remove));
			return ;
		}
		int lastZero = getLastZero(arr);
		for (int j = lastZero; j < arr.length; j++) {
			if(arr[j]==0)
				continue;
			int[] newArr = Arrays.copyOf(arr, arr.length);
			newArr[j] = 0;
			arrangeCompose(newArr, list, i+1,remove);
		}
	}
	private static  int[] getList(int[] arr,int remove){
		int[] newArr=new int[arr.length-remove];
		for (int i = 0,j=0; i < arr.length; i++) {
			if(arr[i]!=0){
				newArr[j++]=arr[i];
			}
		}
		return newArr;
	}
	private static  int getLastZero(int[] arr){
		for (int i = arr.length-1; i >=0 ; i--) {
			if(arr[i]==0)
				return i;
		}
		return 0;
	}
	
}
